package com.mop.app.global;

import com.mop.app.common.exception.Message;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * @description：
 * @author：liqiang
 * @date：2019-07-08 11:09
 * @warning：本内容仅限于杭州阿拉丁信息科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
public class GlobalResponseInitializer implements InitializingBean {

    RequestMappingHandlerAdapter handlerAdapter;

    public GlobalResponseInitializer(RequestMappingHandlerAdapter handlerAdapter) {
        this.handlerAdapter = handlerAdapter;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        decorateValueHandler(handlerAdapter);
    }

    private void decorateValueHandler(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
        List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(returnValueHandlers);
        for (int i = 0; i < handlers.size(); i++) {
            HandlerMethodReturnValueHandler handler = handlers.get(i);
            if (handler instanceof RequestResponseBodyMethodProcessor) {
                handlers.set(i, new GlobalResponseInitializer.GlobalResponseValueHandler(handler));
            }
        }
        requestMappingHandlerAdapter.setReturnValueHandlers(handlers);
    }

    /**
     * @description：统一返回值拦截器
     * @author：liqiang
     * @date：2019-07-01 13:00
     * @warning：本内容仅限于杭州阿拉丁信息科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的
     */
    public static class GlobalResponseValueHandler implements HandlerMethodReturnValueHandler {

        HandlerMethodReturnValueHandler returnValueHandler;

        public GlobalResponseValueHandler(HandlerMethodReturnValueHandler returnValueHandler) {
            this.returnValueHandler = returnValueHandler;
        }

        @Override
        public boolean supportsReturnType(MethodParameter returnType) {
            return this.returnValueHandler.supportsReturnType(returnType);
        }

        @Override
        public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
            if (returnType.getParameterType().isAssignableFrom(void.class)) {
                returnValueHandler.handleReturnValue(Message.success(), returnType, mavContainer, webRequest);
                return;
            }
            if (returnType.hasMethodAnnotation(DisableGlobalResponse.class) ||
                    returnType.getContainingClass().isAnnotationPresent(DisableGlobalResponse.class)) {
                returnValueHandler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
                return;
            }
            if (returnValue instanceof Message) {
                returnValueHandler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
                return;
            }
            Message result = Message.success(returnValue);
            returnValueHandler.handleReturnValue(result, returnType, mavContainer, webRequest);
        }
    }
}
